package com.mxnet.chongjin.supoman.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.mxnet.chongjin.supoman.MainActivity;
import com.mxnet.chongjin.supoman.R;
import com.mxnet.chongjin.supoman.config.Api;
import com.mxnet.chongjin.supoman.utils.StringUtils;
import com.mxnet.chongjin.supoman.utils.TCPUtils;
import com.mxnet.chongjin.supoman.utils.ToastUtils;

public class StartPwdActivity extends AppCompatActivity implements View.OnClickListener, TextWatcher {
    private Context mContext;
    private LinearLayout mLinearLayout;
    private Button mButton;
    private EditText mEditText1, mEditText2, mEditText3, mEditText4;
    private SharedPreferences mPreferences;
    private SharedPreferences.Editor mEditor;
    private int sequenceNum;
    private String MAC;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start_pwd);
        getWindow().getDecorView().setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        getWindow().setStatusBarColor(Color.TRANSPARENT);
        mContext = StartPwdActivity.this;
        mPreferences = getSharedPreferences("supoman", MODE_PRIVATE);
        mEditor = mPreferences.edit();
        MAC = mPreferences.getString("MAC", "");

        initData();
        initView();
    }

    public void initData() {

    }

    public void initView() {
        mLinearLayout = findViewById(R.id.ll_start_pwd);
        mLinearLayout.setOnClickListener(this);

        mButton = findViewById(R.id.btn_set_start_pwd);
        mButton.setOnClickListener(this);

        mEditText1 = findViewById(R.id.et_start_pwd_1);
        mEditText2 = findViewById(R.id.et_start_pwd_2);
        mEditText3 = findViewById(R.id.et_start_pwd_3);
        mEditText4 = findViewById(R.id.et_start_pwd_4);

        mEditText1.addTextChangedListener(this);
        mEditText2.addTextChangedListener(this);
        mEditText3.addTextChangedListener(this);
        mEditText4.addTextChangedListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_start_pwd:
                //
                Intent intent1 = new Intent(mContext, MainActivity.class);
                startActivity(intent1);
                finish();
                break;
            case R.id.btn_set_start_pwd:
                if (!TextUtils.isEmpty(mEditText1.getText()) && !TextUtils.isEmpty(mEditText2.getText()) && !TextUtils.isEmpty(mEditText3.getText()) && !TextUtils.isEmpty(mEditText4.getText())) {
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            sequenceNum = mPreferences.getInt("sequenceNum", 1);
                            String seq = Integer.toHexString(sequenceNum);
                            if (seq.length() == 1) {
                                seq = "000" + seq;
                            } else if (seq.length() == 2) {
                                seq = "00" + seq;
                            } else if (seq.length() == 3) {
                                seq = "0" + seq;
                            }
                            String s = StringUtils.isEmpty(mPreferences.getString("startPwd", null)) ? "ffffffff" : mPreferences.getString("startPwd", "");
                            TCPUtils.TCP_sendMsg(Api.SERVER_IP, Api.SERVER_PORT,
                                    StringUtils.hexStringToBytes(Api.HEAD + MAC+ Api.MY_MAC + s + seq +
                                            Api.PASSWORD +
                                            String.format("%02x", Integer.parseInt(mEditText1.getText().toString())) +
                                            String.format("%02x", Integer.parseInt(mEditText2.getText().toString())) +
                                            String.format("%02x", Integer.parseInt(mEditText3.getText().toString())) +
                                            String.format("%02x", Integer.parseInt(mEditText4.getText().toString()))));
                            if ((sequenceNum + 1) > 65534) {
                                sequenceNum = 0;
                                mEditor.putInt("sequenceNum", sequenceNum);
                            } else {
                                mEditor.putInt("sequenceNum", sequenceNum + 1);
                            }
                            mEditor.putString("startPwd", String.format("%02x", Integer.parseInt(mEditText1.getText().toString())) +
                                    String.format("%02x", Integer.parseInt(mEditText2.getText().toString())) +
                                    String.format("%02x", Integer.parseInt(mEditText3.getText().toString())) +
                                    String.format("%02x", Integer.parseInt(mEditText4.getText().toString())));
                            mEditor.commit();
                            Intent intent = new Intent();
                            intent.putExtra("matched", true);
                            intent.setClass(mContext, MainActivity.class);
                            mContext.startActivity(intent);
                            finish();
                        }
                    }).start();
                } else {
                    ToastUtils.showToast(mContext, "请填写控制密码！");
                }
                break;
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        if (!TextUtils.isEmpty(mEditText1.getText())) {
            if (!TextUtils.isEmpty(mEditText2.getText())) {
                if (!TextUtils.isEmpty(mEditText3.getText())) {
                    if (!TextUtils.isEmpty(mEditText4.getText())) {
                        mEditText4.clearFocus();
                    } else {
                        mEditText1.clearFocus();
                        mEditText2.clearFocus();
                        mEditText3.clearFocus();
                        mEditText4.requestFocus();
                    }
                } else {
                    mEditText1.clearFocus();
                    mEditText2.clearFocus();
                    mEditText3.requestFocus();
                    mEditText4.clearFocus();
                }
            } else {
                mEditText1.clearFocus();
                mEditText2.requestFocus();
                mEditText3.clearFocus();
                mEditText4.clearFocus();
            }
        } else {
            mEditText1.requestFocus();
            mEditText2.clearFocus();
            mEditText3.clearFocus();
            mEditText4.clearFocus();
        }
    }

}
